package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 权限服务类
 */
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService{

    @Autowired
    private PermissionDao permissionDao;

    //查询所有权限信息
    public List<Permission> findAll(){
        return permissionDao.findAll();
    }

    //分页查询权限
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> all = permissionDao.findByCondition(queryString);
        long total = all.getTotal();
        List<Permission> result = all.getResult();
        return new PageResult(total,result);
    }

    //新增权限
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    //根据id查询权限
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    //编辑权限
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    //删除权限
    public void delete(Integer id) {
        //先删除权限和角色的关联关系
        permissionDao.deletePermissionAndRole(id);
        //删除权限
        permissionDao.delete(id);
    }

}
